<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * 
     */
    public function index()
    {
        $banner = Db::name('banner')->field('id,image,link')->where(['type' => '0'])->order('weigh desc')->select();
        $category = Db::name('category')->where(['status' => 'normal','type' => 'goods'])->field('id,name,image')->order('weigh desc')->select();
        foreach($category as $k => $v){
            $goodsCount = Db::name('goods')->where(['category_id' => $v['id'],'status' => 1,'prom_type' => 0])->count();
            $category[$k]['goodsCount'] = $goodsCount;
        }

        $data = [
            'banner' => $banner,
            'category' => $category
        ];
        $this->success('',$data);
    }

    public function category(){
        $category_id = $this->request->request('category_id',0);
        $category = Db::name('category')->where(['status' => 'normal','type' => 'goods'])->field('id,name')->order('weigh desc')->select();
        if($category_id){
            $categoryInfo = Db::name('category')->where(['id' => $category_id])->find();
            $category = Db::name('category')->where(['status' => 'normal','type' => 'goods','id' => ['<>',$category_id]])->field('id,name')->order('weigh desc')->select();
            array_unshift($category,$categoryInfo);

        }
        $this->success('',$category);
    }

    public function buytype(){
        $data = [
            ['id'=>0,'name' => '自己买'],
            ['id'=>1,'name' => '送一人'],
            ['id'=>2,'name' => '群发多人抢']
        ];
    }

    public function test(){
        $order = Db::name('order')->find(78);
        minus_stock($order);
    }

    public function testpay(){
        $openid = 'oDv0D5gVNa2XRdsEllPQZn7M_pEY';
        $fee = 100.00;
        $details = '越南红心火龙果（小）';
        $order_sn = '20180605152818148488';
        $wxpay = new \wechat\WxPay();
        $data = $wxpay->pay($openid,$fee,$details,$order_sn);
        echo json_encode($data);
    }


    public function testscore(){
        $score = intval('-10');
        $update_data = [
//        'score' => ['exp', 'score+', $score]

            "score" => Db::raw("score+$score"),
        ];
        Db::name('user')->where('id', 2)->update($update_data);

        dump(Db::name('user')->getLastSql());
    }

    public function testjihua(){
        file_put_contents('./log/log.php','t'.time().PHP_EOL,FILE_APPEND);
    }

    public function testredis(){
        $config = \think\Config::get('redis');
        $client = new \Predis\Client($config);
//        $key = "lastSearch-" . $this->auth->id;
//        $list = $client->lrange($key,0,-1);

//        $client->set('foo','bar');
        $list = $client->get('foo');
        $this->success('',$list);
    }

    public function testc(){
        $orderM = new \app\common\model\Order();
        $res = $orderM->cartToOrder('20180615152902641161');
        dump($res);
    }

    public function testqrcode(){
        //生成二维码
        $wx = new \wechat\Wx();
        $wxqrce = $wx->getQrCode($this->auth->id,'20180613152887164452');
        dump($wxqrce);exit;
        if($wxqrce['code'] == 0){
            $this->error($wxqrce['msg']);
        }else{
            $this->success();
        }
    }

    public function testswoole(){
        $serv = new swoole_server("0.0.0.0", 9501);
//$serv->on('connect', function ($serv, $fd, $reactor_id){
//	echo "[#".posix_getpid()."]\tClient@[$fd:$reactor_id]: Connect.\n";
//});
        $serv->set(array(
            'worker_num' => 1,

        ));

        $serv->on('receive', function (swoole_server $serv, $fd, $reactor_id, $data) {
            echo "[#".$serv->worker_id."]\tClient[$fd] receive data: $data\n";
            if ($serv->send($fd, "hello {$data}\n") == false)
            {
                echo "error\n";
            }

        });

//$serv->on('close', function ($serv, $fd, $reactor_id) {
//	echo "[#".posix_getpid()."]\tClient@[$fd:$reactor_id]: Close.\n";
//});

        $serv->start();

    }

    

}
